package com.example.sunmiv1.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.CollectRecordInfo;
import com.example.sunmiv1.room.entity.OrderEntity;
import com.luck.picture.lib.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CollectRecordAdapter extends BaseAdapter {

    private Context context;
    private List<CollectRecordInfo.DataBean> list;
    private ViewHolder viewHolder;
    private OnItemClickListener onItemClickListener=null;
    private List<OrderEntity> mOrderList=new ArrayList<>();
    public void setOnItemClickListener(OnItemClickListener listener){
        onItemClickListener=listener;
    }
    public void setOrderList(List<OrderEntity> list){
        mOrderList.clear();
        mOrderList.addAll(list);
    }

    public CollectRecordAdapter(Context context, List<CollectRecordInfo.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.collect_record_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        CollectRecordInfo.DataBean dataBean = list.get(position);
        viewHolder.collectRecordOrderNo.setText(dataBean.getOut_trade_no());
        viewHolder.collectRecordOrderType.setText(dataBean.getOrder_name());
        viewHolder.collectRecordTime.setText(dataBean.getTrade_date());
        viewHolder.collectRecordPayType.setText(dataBean.getPay_name());
        viewHolder.collectRecordMoney.setText(dataBean.getTrade_amount());
        viewHolder.collectRecordPeople.setText(dataBean.getOperator_name());
        viewHolder.remake_tv.setText("");
        for (OrderEntity orderEntity : mOrderList) {
            if(orderEntity.getOrderNo().equals(dataBean.getOut_trade_no())){
                viewHolder.remake_tv.setText(orderEntity.getRemake());
            }
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener!=null){
                    onItemClickListener.onItemClick(null,position);
                }
            }
        });
        return convertView;
    }

    static
    class ViewHolder {
        @BindView(R.id.collectRecord_orderNo)
        TextView collectRecordOrderNo;
        @BindView(R.id.collectRecord_orderType)
        TextView collectRecordOrderType;
        @BindView(R.id.collectRecord_time)
        TextView collectRecordTime;
        @BindView(R.id.collectRecord_payType)
        TextView collectRecordPayType;
        @BindView(R.id.collectRecord_money)
        TextView collectRecordMoney;
        @BindView(R.id.collectRecord_people)
        TextView collectRecordPeople;
        @BindView(R.id.remake_tv)
        TextView remake_tv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
